import { InviteList as pages, LoadFrom, DisposeDeliver, CommunicateResume } from '@/api/resume/resume'
import store from '@/store'

const state = {
  controllerName: 'resumeInvite',
  dataList: {
    listLoading: false,
    list: [],
    educationList: ['不限', '本科及以上', '硕士及以上', '博士/博士后', '大专及以上', '211/985', '统招', '海外留学'],
    addressList: ['全部', '北京', '上海', '广州', '深圳', '河北', '成都', '天津', '南京', '济南', '其他'],
    jobStateList: [
      {
        value: '不限',
        label: '不限'
      },
      {
        value: '1',
        label: '立即到岗'
      },
      {
        value: '2',
        label: '一个月到岗'
      },
      {
        value: '3',
        label: '三个月到岗'
      },
      {
        value: '4',
        label: '六个月到岗'
      }
    ],
    deliverStateList: ['待处理', '已沟通', '已邀面试', '已放弃'],
    defaultProps: {
      children: 'departmentList',
      key: 'id',
      label: 'name'
    },
    searchForm: {
      LoginName: '',
      Address: '',
      SalaryRange: '',
      Education: '',
      JobState: '',
      JobName: '',
      MinWorkHour: '',
      MaxWorkHour: '',
      MinAge: '',
      MaxAge: '',
      pageSize: 10,
      pageIndex: 1,
      dataCount: 0,
      pageCount: 0
    }
  },
  form: {
    formState: false,
    formDepartmentState: false,
    model: {},
    replayState: false,
    replaymodel: {}
  }
}
const mutations = {
  pageSizeChange(state, par) {
    state.dataList.searchForm.pageSize = par
    this.dispatch(`resume/${state.controllerName}/findList`)
  },
  currentPageChange(state, par) {
    state.dataList.searchForm.pageIndex = par
    this.dispatch(`resume/${state.controllerName}/findList`)
  }
}

const actions = {
  loadNewForm(context) {
    var _state = context.state
    _state.form.model = {}
    _state.form.formState = true
  },
  // 加载表单
  loadForm(context, par) {
    var _state = context.state
    console.log('加载表单参数', par)
    var companyId = store.getters.companyId
    console.log(companyId, '公司id')
    LoadFrom({ ID: par.Id, CompanyId: companyId })
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.model = Data
        _state.form.model.deliverId = par.DeliverId
        _state.form.model.deliverState = par.DeliverState
        _state.form.formState = true
      })
      .catch(error => {
        console.log(error)
      })
  },
  findList(context, par) {
    var _state = context.state
    _state.dataList.listLoading = true
    if (_state.dataList.searchForm.MinWorkHour === '') {
      _state.dataList.searchForm.MinWorkHour = 0
    }
    if (_state.dataList.searchForm.MaxWorkHour === '') {
      _state.dataList.searchForm.MaxWorkHour = 0
    }
    if (_state.dataList.searchForm.MinAge === '') {
      _state.dataList.searchForm.MinAge = 0
    }
    if (_state.dataList.searchForm.MaxAge === '') {
      _state.dataList.searchForm.MaxAge = 0
    }
    _state.dataList.searchForm.MinWorkHour = parseInt(_state.dataList.searchForm.MinWorkHour)
    _state.dataList.searchForm.MaxWorkHour = parseInt(_state.dataList.searchForm.MaxWorkHour)
    _state.dataList.searchForm.MinAge = parseInt(_state.dataList.searchForm.MinAge)
    _state.dataList.searchForm.MaxAge = parseInt(_state.dataList.searchForm.MaxAge)
    pages(_state.dataList.searchForm)
      .then(res => {
        const { Data } = res
        console.log(Data)
        _state.dataList.list = Data.List
        _state.dataList.searchForm.pageIndex = Data.PageIndex
        _state.dataList.searchForm.pageSize = Data.PageSize
        _state.dataList.searchForm.dataCount = Data.Count
        _state.dataList.searchForm.pageCount = Data.Pages
        _state.dataList.listLoading = false
      })
      .catch(error => {
        console.log(error)
      })
  },
  // 处理简历
  disposeDeliver(context, par) {
    var _state = context.state
    console.log('处理简历', par)
    DisposeDeliver(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('操作成功!', '成功')
          _state.form.formState = false
          context.dispatch('findList')
        } else {
          global.tools.msg('操作失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  loadReplay(context, par) {
    var _state = context.state
    _state.form.replaymodel.JobId = par
    _state.form.replaymodel.Type = 1
    _state.form.replayState = true
  },
  // 简历留言
  replay(context, par) {
    var _state = context.state
    console.log('简历留言', par)
    CommunicateResume(par)
      .then(response => {
        const { Data } = response
        console.log(Data)
        _state.form.resState = Data
        if (_state.form.resState) {
          global.tools.msg('操作成功!', '成功')
          _state.form.replayState = false
          context.dispatch('findList')
        } else {
          global.tools.msg('操作失败!', '错误')
        }
      })
      .catch(error => {
        console.log(error)
      })
  },
  handleEducation(context, par) {
    console.log(par)
    var _state = context.state
    _state.dataList.searchForm.Education = par
  },
  handleJobState(context, par) {
    console.log(par)
    var _state = context.state
    _state.dataList.searchForm.JobState = par
  },
  handleNodeClick(context, node) {
    var _state = context.state
    _state.form.model.departmentId = node.id
    _state.form.model.departmentName = node.name
    _state.form.formDepartmentState = false
  },
  handleSuccess(context, res) {
    console.log(res)
    if (res.Status === 1) {
      var _state = context.state
      _state.form.model.HeaderImage = res.Data.Url
      console.log('_state.form.model.HeaderImage', _state.form.model.HeaderImage)
    } else {
      global.tools.msg('上传失败，请重新上传！', '提示')
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
